# 
#   Document   : application_helper.rb
#   Created on : Nov 26, 2007, 11:40:16 PM
#   Author     : Geoffrey.Dou   (geoffrey.dou@gmail.com)
#   Description:
#       Common method used in application.
#   Modified log:
#       Name: 
#       Time:
#       Log:
#
module ApplicationHelper
    include TagsHelper
    
    def login?
        session[:user]
    end
  
    def current_user_name
        User.find(:first, :conditions => ["id=?", session[:user]]).login
    end
    
    def blank?(list)
        if list.size == 0
            return false
        else
            return list.size
        end
    end
    
    def format_date(time)
        myformat = '%y年%m月%d日 %H:%M'
        time.strftime(myformat)
    end
    
    def get_user_name(user_id)
        User.find(:first, :conditions => ["id=?", user_id]).login
    end
    
    def collected_count(url_id)
        UsersUrls.find(:all, :conditions => ["url_id=?", url_id]).size
    end
    
    def get_recent_tags_of_url(url_id)
        uu = UsersUrls.find(:first, :conditions => ["url_id=?", url_id], :order => 'collected_at DESC')
        uu.tag_list
    end
    
    def get_taghash_of_url(url_id)
        taghash = {}
        uus = UsersUrls.find(:all, :conditions => ["url_id=?", url_id])
        for uu in uus
            for tag in uu.tag_list
                if taghash.include?(tag)
                    taghash[tag] += 1
                else
                    taghash[tag] = 1
                end
            end
        end
        taghash
    end
    
    def get_all_tags_of_url(url_id)
        get_taghash_of_url(url_id).keys
    end
    
    def get_tag_count(tag_id)
        Tagging.find_all_by_tag_id(tag_id).size
    end    
end
